<?php
namespace Common\Logic;

/**
 * 钟点房逻辑
 * @modify zhangzhen 2017-05-15 创建
 */
class LongRoomLogic extends \Common\Controller\BaseController
{   
    /**
     * 获取客栈列表
     * @modify zhangzhen 2017-05-15 创建
     * @param  integer   $hotelId    客栈ID
     * @return array 钟点房列表
     */
    public function getInfosByHotel($hotelId) {
        $longRoomModel = D('LongRoom');
        $longRoomInfos = $longRoomModel->getInfosJoinName($hotelId);
        if (empty($longRoomInfos)) {
            return false;
        } else {
            $longRoom = [];
            foreach ($longRoomInfos as $roomInfo) {
                $longRoom[$roomInfo['roomtype_id']]['roomid'][] = $roomInfo['roominfo_id'];
                $longRoom[$roomInfo['roomtype_id']]['roomname'][] = $roomInfo['roomname'];
                $longRoom[$roomInfo['roomtype_id']]['price'] =  $roomInfo['longprice'];
                $longRoom[$roomInfo['roomtype_id']]['roomtype'] = $roomInfo['typename'];
                $longRoom[$roomInfo['roomtype_id']]['count']++;
            }
            return $longRoom;
        }
    }

    /**
     * 获取房型实时房态
     * 方法会抛异常
     * @modify trainyao    2016-06-23  创建
     * @modify wenhui      2016-12-21  修改返回price字段值，使用HotelRoomTypePrice表中的预定价格，增加门市价格
     * @modify zhangzhen   2017-3-15   增加尾房特卖价格
     * @param  integer $hotelId 客栈ID
     * @param  string $startDate 查询开始日期
     * @param  string $endDate 查询结束日期
     * @param int $hour
     * @param $needGallery
     * @param null $roomType
     * @param  mixin $roomIds 房间ID,默认查全部
     * @return array 客栈信息数组,失败返回空数组
     *                             [{
     *                                   roomtype_id
     *                                   name
     *                                   price,coverimageurl...等等房型上架信息
     *                                   gallery 房型相册
     *                                   rest 房型剩余可预定数目
     *                                   room array( 房间实时房态id, name...等房间信息 canBook 房间是否可预定)
     *                              }]
     * @throws \Exception
     * @internal param array|null $roomTypeId 需要查询的房型ID,null默认查客栈全部房型
     */
    public function getRealtimeStatus($hotelId, $startDate, $endDate, $month, $needGallery = flase, $roomType = null, $roomIds = null) {
        $longRoomModel = D('LongRoom');
        $longRoomInfos = $longRoomModel->getInfosByHotel($hotelId);
        $roomTypeId = array_column($longRoomInfos, 'roomtype_id');
        $roomTypeId = array_unique($roomTypeId);

        if (isset($roomType) && in_array($roomType, $roomTypeId)) {
            $roomTypeId =[$roomType];
        }

        $roomTypeDetailModel = D('RoomTypeDetail');
        $roomTypeDetail = $roomTypeDetailModel->getInfoByRoomType(null,$roomTypeId,1);

        if (empty($roomTypeDetail)) {
            return array();
        }

        if (count($roomTypeId) == 1) {
            $roomTypeDetail = array($roomTypeDetail);

        }

        $roomTypeId = array_column($roomTypeDetail, 'roomtype_id');

        if ($needGallery) {
            // 获取房型相册信息
            $roomTypePictureModel = D('RoomTypePicture');
            $roomTypePictureInfo = $roomTypePictureModel->getInfoByRoomType($roomTypeId);
            if (!empty($roomTypePictureInfo)) {
                $roomTypePictureInfo = getArrayValueAsIndex($roomTypePictureInfo, 'roomtype_id', 'multi');
            }
        }

        // 获取房型信息
        $roomTypeModel = D('RoomType');
        $roomTypeInfo = $roomTypeModel->getInfo($roomTypeId);
        if (count($roomTypeId) == 1) {
            $roomTypeInfo = array($roomTypeInfo);
        }
        $roomTypeInfo = getArrayValueAsIndex($roomTypeInfo, 'id');
        if (is_null($roomIds)) {
            $roomIds =[];
            foreach ($longRoomInfos as $longRoomInfo) {
                if (in_array($longRoomInfo['roomtype_id'], $roomTypeId)) {
                    $roomIds[]=$longRoomInfo['roominfo_id'];
                }
            }
        }
        // 获取时间段内,相关房间的订单
        $roomBookLogic = A('Common/RoomBook', 'Logic');
        try {
            //endDate跨一天获取间夜房
            $roomBookInfo = $roomBookLogic->getInfo($hotelId, $roomIds, $startDate, $endDate);
        } catch (\Exception $e) {
            throw $e;
            return;
        }
        foreach ($roomTypeDetail as $index => &$roomTypeDetailItem) {            
            if (! isset($roomTypeInfo[$roomTypeDetailItem['roomtype_id']])) {
                unset($roomTypeDetail[$index]);
                continue;
            }
            $roomTypeDetailItem['name'] = $roomTypeInfo[$roomTypeDetailItem['roomtype_id']]['name'];
            $roomTypeDetailItem['price'] = $roomTypeInfo[$roomTypeDetailItem['roomtype_id']]['longprice'] * $month;
            $roomTypeDetailItem['charityprice'] = $roomTypeInfo[$roomTypeDetailItem['roomtype_id']]['charityprice'];
            if ($needGallery) {
                if (isset($roomTypePictureInfo[$roomTypeDetailItem['roomtype_id']])) {
                    $roomTypeDetailItem['gallery'] = $roomTypePictureInfo[$roomTypeDetailItem['roomtype_id']];
                } else {
                    $roomTypeDetailItem['gallery'] = array();
                }
            }
            if (isset($roomBookInfo[$roomTypeDetailItem['roomtype_id']])) {
                $roomTypeDetailItem['rest'] = $roomBookInfo[$roomTypeDetailItem['roomtype_id']]['rest'];
                $roomTypeDetailItem['room'] = $roomBookInfo[$roomTypeDetailItem['roomtype_id']]['room'];
            } else {
                $roomTypeDetailItem['rest'] = 0;
                $roomTypeDetailItem['room'] = array();
            }
        }
        unset($index);
        unset($roomTypeDetailItem);
        return $roomTypeDetail;
    }
}
